package com.nanhu.test.widget;

import com.nanhu.test.R;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class EditCancel extends LinearLayout implements EdtInterface{
	
	private ImageButton ib;
	
	private EditText et;
	
	public EditCancel(Context context){
		this(context, null);
	}
	
	public EditCancel(Context context, AttributeSet attrs){
		super(context, attrs);
		LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
		init();
	}
	
	private void init() {
		ib = (ImageButton) findViewById(R.id.ib);
		et = (EditText) findViewById(R.id.et);
		et.addTextChangedListener(new TextWatcher(){

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void afterTextChanged(Editable s) {
				if(s.length() == 0){
					hideBtn();
				}else{
					showBtn();
				}
			}
		});
		
		ib.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				et.setText("");// 设置输入框内容为空
			}
			
		});
	}

	@Override
	public void hideBtn() {
		if(ib.isShown())ib.setVisibility(View.GONE);
	}

	@Override
	public void showBtn() {
		if(!ib.isShown())ib.setVisibility(View.VISIBLE);
	}
	
}

interface EdtInterface {

    public void hideBtn();

    public void showBtn();

}